package cn.gson.crm.controller.counselor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.Jdbc;
import cn.gson.crm.model.dao.StudentPoDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.po.FollowStudentPo;

@WebServlet("/counselor/mycustomer")
public class MyCustomerServlet extends BaseServlet {
	/**
	 * 咨询师的客户管理界面
	 */
	private static final long serialVersionUID = 1L;
	private StudentPoDao stuPoDao = new StudentPoDao();

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Employee emp = (Employee) request.getSession().getAttribute("emplo");
			String[] cusClass = request.getParameterValues("class");// 筛选不同等级的客户
			String p = request.getParameter("page");
			if (p == null || p.isEmpty()) {
				p = "1";
			}
			Integer page = Integer.parseInt(p);
			//输入名字查询(分页)
			String stuName = request.getParameter("stuName");
				// 判断是否是筛选，cusClass为空时 显示所有潜在学员列表
				if (cusClass == null) {
					/**
					 * 获取咨询师上一次记录的下一跟进时间，先获取到客户集合，
					 * 再根据咨询师id和学生id分别查询查询每一个客户记录列表，
					 * 将获取到的第一记录的下一次跟进时间，set进po对象，po对象再加入新集合
					 * 新集合再分页
					 */
					
					List<FollowStudentPo> getAllFollowStu1 = stuPoDao.getFollowStudentList(emp.getId(), "正常", "已分配", stuName);
					List<FollowStudentPo> getAllFollowStu =null;
					if(getAllFollowStu1!=null){
						getAllFollowStu = new ArrayList<>(); 
						for (FollowStudentPo po : getAllFollowStu1) {
							List<FollowStudentPo> recordList = stuPoDao.getFollowRecordById(po.getId(), emp.getId());
							if(recordList!=null&&recordList.size()>0){
								if(recordList.get(0) != null){
									po.setNextTime(recordList.get(0).getNextTime());
								}
							}
							getAllFollowStu.add(po);
						}
					}
					List<FollowStudentPo> followStuList= stuPoDao.getListThenDividPage(request, getAllFollowStu, page);
					request.setAttribute("followStuList", followStuList);
				} else {
					// 否则将url栏内所有查询条件分别加入conStuList集合，然后遍历所有元素将几个条件的学生加入allConStudentList中
					List<FollowStudentPo> allConStudentList = new ArrayList<>();
					// Integer count=0;
					for (int i = 0; i < cusClass.length; i++) {
						List<FollowStudentPo> conStuList = stuPoDao.getFilterStudentList(emp.getId(), "正常","已分配", cusClass[i]);
						if (conStuList != null) {
							for (FollowStudentPo po : conStuList) {
								if (po != null) {
									allConStudentList.add(po);
								}
							}
						}
						if (allConStudentList != null) {
							List<FollowStudentPo> followStuList = stuPoDao.getListThenDividPage(request, allConStudentList, page);
							request.setAttribute("followStuList", followStuList);
						}
						}
					}
			
			// 获取url并且跳转
			request.setAttribute("page", page);
			request.setAttribute("queryString", noPageQueryString(request));
			getServletPathAndForward(request, "counselor/cou_manage.jsp");
	}
	
}
